package com.member.web.controller.shop;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.member.common.annotation.Log;
import com.member.common.core.controller.BaseController;
import com.member.common.core.domain.AjaxResult;
import com.member.common.core.page.TableDataInfo;
import com.member.common.enums.BusinessType;
import com.member.common.enums.OrderDeleteEnum;
import com.member.common.enums.UpDownShelvesEnum;
import com.member.common.utils.StringUtils;
import com.member.common.utils.poi.ExcelUtil;
import com.member.shop.domain.SysBizJingang;
import com.member.shop.domain.SysBizMemberGrade;
import com.member.shop.domain.SysBizUser;
import com.member.shop.mapper.SysBizUserMapper;
import com.member.shop.service.ISysBizMemberGradeService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 【会员等级】控制器
 *
 * @author lixinchen
 * @version 1.0
 * @date 2024-12-29
 **/
@RestController
@RequestMapping("/shop/grade")
public class SysBizMemberGradeController extends BaseController {
    @Resource
    private ISysBizMemberGradeService sysBizMemberGradeService;

    private void setTenantId(SysBizMemberGrade sysBizMemberGrade) {
        Long tenantId = getTenantId();
        if (tenantId != null && tenantId > 0) {
            sysBizMemberGrade.setTenantId(tenantId);
        }
    }

    /**
     * 查询会员等级列表
     */
    @PreAuthorize("@ss.hasPermi('shop:grade:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysBizMemberGrade sysBizMemberGrade) {
        startPage();
        Long tenantId = getTenantId();
        LambdaQueryWrapper<SysBizMemberGrade> queryWrapper = new LambdaQueryWrapper<SysBizMemberGrade>();
        if (tenantId != null && tenantId > 0) {

            queryWrapper.eq(SysBizMemberGrade::getTenantId, tenantId);
        }
        queryWrapper.like(StringUtils.isNotEmpty(sysBizMemberGrade.getGradeName()), SysBizMemberGrade::getGradeName, sysBizMemberGrade.getGradeName());
        queryWrapper.like(sysBizMemberGrade.getDisplayed() != null, SysBizMemberGrade::getDisplayed, sysBizMemberGrade.getDisplayed());
        queryWrapper.eq(SysBizMemberGrade::getDeleted, OrderDeleteEnum.NOT_DELETE.getKey());
        queryWrapper.orderByDesc(SysBizMemberGrade::getCreateTime);
        List<SysBizMemberGrade> list = sysBizMemberGradeService.selectSysBizMemberGradeList(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 查询会员等级列表
     */
    @PreAuthorize("@ss.hasPermi('shop:grade:list')")
    @GetMapping("/listGradeAll")
    public TableDataInfo listGradeAll(SysBizMemberGrade sysBizMemberGrade) {
        startPage();
        LambdaQueryWrapper<SysBizMemberGrade> queryWrapper = new LambdaQueryWrapper<SysBizMemberGrade>();
        queryWrapper.like(StringUtils.isNotEmpty(sysBizMemberGrade.getGradeName()), SysBizMemberGrade::getGradeName, sysBizMemberGrade.getGradeName());
        queryWrapper.like(sysBizMemberGrade.getDisplayed() != null, SysBizMemberGrade::getDisplayed, sysBizMemberGrade.getDisplayed());
        queryWrapper.eq(SysBizMemberGrade::getDeleted, OrderDeleteEnum.NOT_DELETE.getKey());
        queryWrapper.eq(SysBizMemberGrade::getDisplayed, UpDownShelvesEnum.GROUNDING.getKey());
        queryWrapper.orderByDesc(SysBizMemberGrade::getCreateTime);
        List<SysBizMemberGrade> list = sysBizMemberGradeService.selectSysBizMemberGradeList(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 导出会员等级列表
     */
    @PreAuthorize("@ss.hasPermi('shop:grade:export')")
    @Log(title = "会员等级", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysBizMemberGrade sysBizMemberGrade) {
        LambdaQueryWrapper<SysBizMemberGrade> queryWrapper = new LambdaQueryWrapper<SysBizMemberGrade>();
        List<SysBizMemberGrade> list = sysBizMemberGradeService.selectSysBizMemberGradeList(queryWrapper);
        ExcelUtil<SysBizMemberGrade> util = new ExcelUtil<SysBizMemberGrade>(SysBizMemberGrade.class);
        util.exportExcel(response, list, "会员等级数据");
    }

    /**
     * 获取会员等级详细信息
     */
    @PreAuthorize("@ss.hasPermi('shop:grade:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(sysBizMemberGradeService.selectSysBizMemberGradeById(id));
    }

    /**
     * 新增会员等级
     */
    @PreAuthorize("@ss.hasPermi('shop:grade:add')")
    @Log(title = "会员等级", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysBizMemberGrade sysBizMemberGrade) {
        LambdaQueryWrapper<SysBizMemberGrade> queryWrapper = new LambdaQueryWrapper<SysBizMemberGrade>();
        queryWrapper.eq(SysBizMemberGrade::getDeleted, OrderDeleteEnum.NOT_DELETE.getKey());
        queryWrapper.eq(!sysBizMemberGrade.getGradeName().isEmpty(), SysBizMemberGrade::getGradeName, sysBizMemberGrade.getGradeName());
        List<SysBizMemberGrade> list = sysBizMemberGradeService.selectSysBizMemberGradeList(queryWrapper);
        if (!list.isEmpty()) {
            return warn("该会员等级名称已存在");
        }
        int i = sysBizMemberGradeService.insertSysBizMemberGrade(sysBizMemberGrade);

        return toAjax(i);
    }

    @Resource
    private SysBizUserMapper sysBizUserMapper;

    /**
     * 修改会员等级
     */
    @PreAuthorize("@ss.hasPermi('shop:grade:edit')")
    @Log(title = "会员等级", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysBizMemberGrade sysBizMemberGrade) {
        LambdaQueryWrapper<SysBizMemberGrade> queryWrapper = new LambdaQueryWrapper<SysBizMemberGrade>();
        queryWrapper.eq(SysBizMemberGrade::getDeleted, OrderDeleteEnum.NOT_DELETE.getKey());
        queryWrapper.ne(sysBizMemberGrade.getId() != null, SysBizMemberGrade::getId, sysBizMemberGrade.getId());
        queryWrapper.eq(!sysBizMemberGrade.getGradeName().isEmpty(), SysBizMemberGrade::getGradeName, sysBizMemberGrade.getGradeName());
        List<SysBizMemberGrade> list = sysBizMemberGradeService.selectSysBizMemberGradeList(queryWrapper);
        if (!list.isEmpty()) {
            return warn("该会员等级名称已存在");
        }

        if (sysBizMemberGrade.getDisplayed() == (long) UpDownShelvesEnum.OFF_SHELF.getKey()) {
            //查看是否有用户使用该会员
            LambdaQueryWrapper<SysBizUser> sysBizUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
            sysBizUserLambdaQueryWrapper.eq(SysBizUser::getMemberGradeId, sysBizMemberGrade.getId());
            List<SysBizUser> sysBizUsers = sysBizUserMapper.selectList(sysBizUserLambdaQueryWrapper);
            if (!sysBizUsers.isEmpty()) {
                return warn("该会员等级已经被用户使用，请先解绑");
            }
        }
        return toAjax(sysBizMemberGradeService.updateSysBizMemberGrade(sysBizMemberGrade));
    }

    /**
     * 删除会员等级
     */
    @PreAuthorize("@ss.hasPermi('shop:grade:remove')")
    @Log(title = "会员等级", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        //查看是否有用户使用该会员
        LambdaQueryWrapper<SysBizUser> sysBizUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysBizUserLambdaQueryWrapper.eq(SysBizUser::getMemberGradeId, id);
        List<SysBizUser> sysBizUsers = sysBizUserMapper.selectList(sysBizUserLambdaQueryWrapper);
        if (!sysBizUsers.isEmpty()) {
            return warn("该会员等级已经被用户使用，请先解绑");
        }
        return toAjax(sysBizMemberGradeService.deleteSysBizMemberGradeById(id));
    }
}
